package com.fulihui.wgj.dal.mybatis.repository;

import com.fulihui.wgj.dal.mybatis.domain.WxSubscribeStatistics;
import com.fulihui.wgj.dal.mybatis.mapper.WxSubscribeStatisticsMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author wang_wx
 * @date 2018-07-05
 */
@Component
public class WxSubscribeStatisticsRepository extends BaseRepository<WxSubscribeStatistics> {

    private static final Logger LOGGER = LoggerFactory.getLogger(WxSubscribeStatisticsRepository.class);

    @Resource
    WxSubscribeStatisticsMapper wxSubscribeStatisticsMapper;

    /**
     * 查询是否存在 -- 根据appId 和 date
     *
     * @param subscribeStatistics
     * @return
     */
    public int countByAppIdAndDate(WxSubscribeStatistics subscribeStatistics) {
        Example example = bulidExample(subscribeStatistics);
        return wxSubscribeStatisticsMapper.selectCountByExample(example);
    }

    /**
     * 查询列表
     *
     * @param appId     公众号appId
     * @param beginDate 开始时间
     * @param endDate   结束时间
     * @return
     */
    public List<WxSubscribeStatistics> queryList(String appId, String beginDate, String endDate) {
        Example example = new Example(WxSubscribeStatistics.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(appId)) {
            criteria.andEqualTo("appId", appId);
        }
        if (StringUtils.isNotBlank(beginDate)) {
            criteria.andGreaterThanOrEqualTo("refDate", beginDate);
        }
        if (StringUtils.isNotBlank(endDate)) {
            criteria.andLessThanOrEqualTo("refDate", endDate);
        }
        example.orderBy("refDate").asc();
        return wxSubscribeStatisticsMapper.selectByExample(example);
    }

    /**
     * 查询列表-分页
     *
     * @param appId     公众号appId
     * @param beginDate 开始时间
     * @param endDate   结束时间
     * @param page
     * @param rows
     * @return
     */
    public PageInfo<WxSubscribeStatistics> queryPageList(String appId, String beginDate, String endDate, Integer page, Integer rows) {
        Example example = new Example(WxSubscribeStatistics.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(appId)) {
            criteria.andEqualTo("appId", appId);
        }
        if (StringUtils.isNotBlank(beginDate)) {
            criteria.andGreaterThanOrEqualTo("refDate", beginDate);
        }
        if (StringUtils.isNotBlank(endDate)) {
            criteria.andLessThanOrEqualTo("refDate", endDate);
        }
        example.orderBy("refDate").asc();
        PageHelper.startPage(page, rows);
        List<WxSubscribeStatistics> subscribeStatistics = wxSubscribeStatisticsMapper.selectByExample(example);
        return new PageInfo<>(subscribeStatistics);
    }

    /**
     * 更新 -- 根据appId 和 date
     *
     * @param wxSubscribeStatistics
     * @return
     */
    public int updateByAppIdAndDate(WxSubscribeStatistics wxSubscribeStatistics) {
        if (StringUtils.isNotBlank(wxSubscribeStatistics.getAppId()) || StringUtils.isNotBlank(wxSubscribeStatistics.getRefDate())) {
            LOGGER.warn("appId或者date为空，不进行更新操作！");
            return 0;
        }
        wxSubscribeStatistics.setGmtModified(new Date());
        Example example = bulidExample(wxSubscribeStatistics);
        return wxSubscribeStatisticsMapper.updateByExampleSelective(wxSubscribeStatistics, example);
    }

    /**
     * 组装条件
     *
     * @param subscribeStatistics
     * @return
     */
    Example bulidExample(WxSubscribeStatistics subscribeStatistics) {
        if (Objects.nonNull(subscribeStatistics)) {
            Example example = new Example(WxSubscribeStatistics.class);
            Example.Criteria criteria = example.createCriteria();
            String appId = subscribeStatistics.getAppId();
            String refDate = subscribeStatistics.getRefDate();
            if (StringUtils.isNotBlank(appId)) {
                criteria.andEqualTo("appId", appId);
            }
            if (StringUtils.isNotBlank(refDate)) {
                criteria.andEqualTo("refDate", refDate);
            }
            return example;
        }
        return null;
    }
}
